package com.yunfeisoft.business.service.impl;

import com.yunfeisoft.base.dao.inter.AttachmentDao;
import com.yunfeisoft.base.model.Attachment;
import com.yunfeisoft.business.dao.inter.ElderApprovalResultDao;
import com.yunfeisoft.business.dao.inter.ElderDao;
import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.business.model.ElderApprovalResult;
import com.yunfeisoft.business.service.inter.ElderApprovalResultService;
import com.yunfeisoft.common.base.BaseServiceImpl;
import com.yunfeisoft.common.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Auther: r.cheng
 * @Date: 2020/4/1 11:06
 * @Description: 老人入院审批service实现
 * @Version: 1.0
 */
@Service("elderApprovalResultService")
public class ElderApprovalResultServiceImpl extends BaseServiceImpl<ElderApprovalResult, String, ElderApprovalResultDao> implements ElderApprovalResultService {

    @Autowired
    private ElderDao elderDao;

    @Autowired
    private AttachmentDao attachmentDao;

    /**
     * 添加医康部审批记录
     * @param approvalResult
     * @return
     */
    @Transactional
    public int doctorSave(ElderApprovalResult approvalResult, String[] attachmentIds) {
        // 添加审批记录
        save(approvalResult);
        // 保存审批附件和审批记录关联关系
        if (!ObjectUtils.isEmpty(attachmentIds)) {
            attachmentDao.updateRefId(attachmentIds, approvalResult.getId());
        }
        // 获取审批状态
        Integer status = approvalResult.getStatus();
        Elder elder = new Elder();
        elder.setId(approvalResult.getElderId());
        if (ElderApprovalResult.ApprovalStatusEnum.ACCEPT.getValue() == status) {
            // 医康部审批通过
            elder.setStatus(Elder.ElderStatusEnum.DOCTOR_ACCEPT.getValue());
        } else {
            // 医康部审批拒绝
            elder.setStatus(Elder.ElderStatusEnum.DOCTOR_REJECT.getValue());
        }
        return elderDao.update(elder);
    }

    /**
     * 添加护理部审批记录
     * @param params
     * @return
     */
    @Transactional
    public int nurseSave(Map<String, Object> params, String[] attachmentIds) {
        // 添加审批记录
        String elderId = (String) params.get("elderId");
        String content = (String) params.get("content");
        Integer status = (Integer) params.get("status");
        String nursingLevel = (String) params.get("nursingLevel");
        ElderApprovalResult approvalResult = new ElderApprovalResult();
        approvalResult.setElderId(elderId);
        approvalResult.setContent(content);
        approvalResult.setStatus(status);
        // 保存审批记录
        save(approvalResult);
        // 保存审批附件和审批记录关联关系
        if (!ObjectUtils.isEmpty(attachmentIds)) {
            attachmentDao.updateRefId(attachmentIds, approvalResult.getId());
        }
        Elder elder = new Elder();
        elder.setId(elderId);
        if (ElderApprovalResult.ApprovalStatusEnum.ACCEPT.getValue() == status) {
            // 审批通过，设置状态，更新护理等级
            elder.setNursingLevel(Integer.valueOf(nursingLevel));
            elder.setStatus(Elder.ElderStatusEnum.NURSE_ACCEPT.getValue());
            // 更新Elder状态
            return elderDao.update(elder);
        } else if (ElderApprovalResult.ApprovalStatusEnum.REJECT.getValue() == status) {
            // 审批拒绝，设置状态
            elder.setStatus(Elder.ElderStatusEnum.NURSE_REJECT.getValue());
            // 更新Elder状态
            return elderDao.update(elder);
        }
        return 0;
    }

    /**
     * 添加院办审批记录
     * @param approvalResult
     * @return
     */
    @Transactional
    public int officeSave(ElderApprovalResult approvalResult, String[] attachmentIds) {
        // 添加审批记录
        save(approvalResult);
        // 保存审批附件和审批记录关联关系
        if (!ObjectUtils.isEmpty(attachmentIds)) {
            attachmentDao.updateRefId(attachmentIds, approvalResult.getId());
        }
        // 获取审批状态
        Integer status = approvalResult.getStatus();
        Elder elder = new Elder();
        elder.setId(approvalResult.getElderId());
        if (ElderApprovalResult.ApprovalStatusEnum.ACCEPT.getValue() == status) {
            // 院办审批通过
            elder.setStatus(Elder.ElderStatusEnum.CONTRACT_ACCEPT.getValue());
        } else {
            // 院办审批拒绝
            elder.setStatus(Elder.ElderStatusEnum.CONTRACT_REJECT.getValue());
        }
        return elderDao.update(elder);
    }

    /**
     * 查询审批列表
     * @param elderId
     * @return
     */
    @Override
    public List<ElderApprovalResult> queryByElderId(String elderId) {
        return getDao().queryByElderId(elderId);
    }


}
